﻿@page "/systemset/role"
@using AdminShared.Models.Role
@using System.ComponentModel
@attribute [ReuseTabsPageTitle("角色管理")]

<Layout Style="padding-top:30px;">

    <Content Class="site-layout-background">

        <div style="margin-bottom:10px">

            @if (MainLayout.functionList.Contains("role-add"))
            {
                <Button Icon="plus" Type="@ButtonType.Primary" @onclick="()=>EditRole()">添加</Button>
            }

        </div>
        <Table TItem="DtoRole" DataSource="@pageList.List" Total="@pageList.Total" Loading="tableLoading" RemoteDataSource>

            <ColumnDefinitions>
                <Column @bind-Field="@context.Name" Title="名称"></Column>
                <Column @bind-Field="@context.Remarks" Title="备注"></Column>
                <Column Field="@context.CreateTime.LocalDateTime" Format="yyyy-MM-dd HH:mm" Title="创建时间">
                </Column>
                <ActionColumn Title="操作">
                    <Space>

                        @if (MainLayout.functionList.Contains("role-edit"))
                        {
                            <SpaceItem>
                                <a @onclick="()=>EditRole(context)">编辑</a>
                            </SpaceItem>
                        }

                        @if (MainLayout.functionList.Contains("role-function"))
                        {
                            <SpaceItem>
                                <a @onclick="()=>EditAuthority(context.Id)" style="color:darkorchid">权限范围</a>
                            </SpaceItem>
                        }

                        @if (MainLayout.functionList.Contains("role-delete"))
                        {
                            <SpaceItem>
                                <Popconfirm Title="确认要删除吗？"
                                        OnConfirm="_=>DeleteRole(context.Id)"
                                        OkText="Yes"
                                        CancelText="No">
                                    <a style="color:red">删除</a>
                                </Popconfirm>

                            </SpaceItem>
                        }

                    </Space>
                </ActionColumn>
            </ColumnDefinitions>

            <PaginationTemplate>
                <div style="margin:15px 0;float:right">
                    <Pagination Total="@pageList.Total" PageSize="pageSize" Current="pageNum" ShowSizeChanger OnChange="PageChange" ShowQuickJumper ShowTotal="showTotal" />
                </div>
            </PaginationTemplate>

        </Table>

    </Content>
</Layout>


<Drawer Closable="true" Width="380" Visible="isShowEditRole" Title='("角色信息")' OnClose="()=>isShowEditRole=false">
    <Template style="height:90%">

        <Form Model="@editRole" OnFinish="SaveRole">

            <FormItem>
                <Text>名称</Text>
                <Input Placeholder="请输入名称" @bind-Value="@context.Name" />
            </FormItem>

            <FormItem>
                <Text>备注</Text>
                <Input Placeholder="请输入备注" @bind-Value="@context.Remarks" />
            </FormItem>



            <Row Gutter="24">
                <AntDesign.Col Span="24">
                    <div style="float:right">
                        <Button Type="@ButtonType.Primary" HtmlType="submit" Loading="@saveLoading">保存</Button>
                        <Button Type="@ButtonType.Default" @onclick="()=>isShowEditRole=false">取消</Button>
                    </div>
                </AntDesign.Col>
            </Row>
        </Form>
    </Template>
</Drawer>


<Modal Title="权限范围" Style="width:1160px;" Visible="@isShowEditAuthority" OnCancel="()=>isShowEditAuthority=false" Footer="null" MaxBodyHeight="@("70vh")">
    <Table DataSource="roleFunctionList" TreeChildren="item=>item.ChildList" DefaultExpandAllRows HidePagination>

        <Column Title="菜单模块" TData="string">
            <Checkbox Label="@context.Id.ToString()" @bind-Checked="@context.IsCheck" OnChange="isCheck=>SetRoleFunction(context.Id,isCheck)">@context.Name</Checkbox>
        </Column>

        <Column Title="功能操作" TData="string">
            <Row>
                @foreach (var item in context.FunctionList)
                {
                    <Checkbox Label="@item.Id.ToString()" @bind-Checked="@item.IsCheck" OnChange="isCheck=>SetRoleFunction(item.Id,isCheck)">@item.Name</Checkbox>
                }
            </Row>
        </Column>

    </Table>

</Modal>


@code {

    override protected void OnInitialized()
    {
        GetRoleList();
    }

    bool saveLoading = false;
    bool tableLoading = false;
    int pageNum = 1;
    int pageSize = 10;


    bool isShowEditAuthority = false;


    DtoPageList<DtoRole> pageList = new();

    List<DtoRoleFunction> roleFunctionList = new();

    async void GetRoleList()
    {
        tableLoading = true;
        var retData = await Http.GetFromJsonAsync<DtoPageList<DtoRole>>("Role/GetRoleList?pageNum=" + pageNum + "&pageSize=" + pageSize);

        if (retData != null)
        {
            pageList = retData;
        }

        tableLoading = false;
        StateHasChanged();
    }

    void PageChange(PaginationEventArgs args)
    {
        if (pageNum != args.Page)
        {
            pageNum = args.Page;
        }

        if (pageSize != args.PageSize)
        {
            pageSize = args.PageSize;
        }

        GetRoleList();
    }

    Func<PaginationTotalContext, string> showTotal = pageList => $"共 {pageList.Total} 条";


    bool isShowEditRole = false;

    long roleId;
    DtoEditRole editRole = new();

    void EditRole(DtoRole? role = null)
    {
        roleId = default;
        editRole = new DtoEditRole();

        if (role != null)
        {
            roleId = role.Id;
            editRole.Name = role.Name;
            editRole.Remarks = role.Remarks;
        }


        isShowEditRole = true;

        StateHasChanged();
    }

    async void SaveRole()
    {
        saveLoading = true;
        if (roleId == default)
        {
            using (var httpResponse = await Http.PostAsJsonAsync<DtoEditRole>("Role/CreateRole", editRole))
            {
                var createRoleId = httpResponse.Content.ReadAsStringAsync().Result;

                Message.Success("添加成功");
            }
        }
        else
        {
            using (var httpResponse = await Http.PostAsJsonAsync<DtoEditRole>("Role/UpdateRole?roleId=" + roleId, editRole))
            {
                var updateLinkRet = httpResponse.Content.ReadAsStringAsync().Result;

                if (bool.Parse(updateLinkRet))
                {
                    Message.Success("编辑成功");
                }
            }
        }
        saveLoading = false;

        GetRoleList();
        isShowEditRole = false;
    }


    async void DeleteRole(long roleId)
    {
        using (var httpResponse = await Http.DeleteAsync("Role/DeleteRole?id=" + roleId))
        {
            var retValue = httpResponse.Content.ReadAsStringAsync().Result;

            if (Convert.ToBoolean(retValue))
            {
                GetRoleList();
                Message.Success("删除成功");
            }
        }
    }


    async void EditAuthority(long roleId)
    {
        this.roleId = roleId;

        var retData = await Http.GetFromJsonAsync<List<DtoRoleFunction>>("Role/GetRoleFunction?roleId=" + roleId);

        if (retData != null)
        {
            roleFunctionList = retData;
        }

        isShowEditAuthority = true;

        StateHasChanged();
    }


    async void SetRoleFunction(long functionId, bool isCheck)
    {
        DtoSetRoleFunction setRoleFunction = new()
            {
                RoleId = roleId,
                FunctionId = functionId,
                IsCheck = isCheck
            };

        using (var httpResponse = await Http.PostAsJsonAsync<DtoSetRoleFunction>("Role/SetRoleFunction", setRoleFunction))
        {
            var ret = httpResponse.Content.ReadAsStringAsync().Result;

            if (bool.Parse(ret))
            {
                Message.Success("设置成功");
            }
        }
    }


}
